package edu.unicauca.opentextbook.utilil;

import edu.unicauca.opentextbook.entidades.Pertenece;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.primefaces.context.RequestContext;

public class Utilidades {

    private Utilidades() {
        super();
    }

    public static Pertenece clonarObjecto(Pertenece objecto) {
        Pertenece objectoClonado = null;
        if (objecto == null) {
            return null;
        } else {
            try {
                objectoClonado = (Pertenece) SerializationUtils.clone(objecto);
            } catch (Exception e) {
                objectoClonado = null;
            } finally {
                return objectoClonado;
            }
        }
    }

    public static <T> List<T> clonarListaObjectos(List<T> listaObjectos) {
        List<T> listaObjectosClonados = new ArrayList<T>();
        T objectoClonado = null;
        try {
            for (T objecto : listaObjectos) {
                objectoClonado = (T) BeanUtils.cloneBean(objecto);
                listaObjectosClonados.add(objectoClonado);
            }

        } catch (IllegalAccessException illegalAccessException) {
            listaObjectosClonados = null;
        } catch (InstantiationException instantiationException) {
            listaObjectosClonados = null;
        } catch (InvocationTargetException invocationTargetException) {
            listaObjectosClonados = null;
        } catch (NoSuchMethodException noSuchMethodException) {
            listaObjectosClonados = null;
        } catch (UnsupportedOperationException unsupportedOperationException) {
            listaObjectosClonados = null;
        } catch (ClassCastException classCastException) {
            listaObjectosClonados = null;
        } catch (IllegalArgumentException illegalArgumentException) {
            listaObjectosClonados = null;
        }
        return listaObjectosClonados;
    }

    public static void ejecutarUpdate(String id) {
        final RequestContext context = RequestContext.getCurrentInstance();
        context.update(id);
    }

    public static UIComponent buscarHtmlComponete(String idComponete) {
        final FacesContext context = FacesContext.getCurrentInstance();
        if (null != context) {
            return buscarHtmlComponete(context.getViewRoot(), idComponete);
        }
        return null;
    }

    public static UIComponent buscarHtmlComponete(UIComponent parent,
                                                  String idComponete) {
        if (idComponete.equals(parent.getId())) {
            return parent;
        }
        final Iterator<UIComponent> kids = parent.getFacetsAndChildren();
        while (kids.hasNext()) {
            final UIComponent kid = kids.next();
            final UIComponent found = buscarHtmlComponete(kid, idComponete);
            if (found != null) {
                return found;
            }
        }
        return null;
    }

    public static <T> boolean validaNulos(T entidad) {
        if (entidad == null) {
            return false;
        }
        return true;
    }

    public static void ejecutarJavaScript(String js) {
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute(js);
    }
}
